package database

import (
	"cabinet/model"
	"gorm.io/gorm"
)

func Migrate(db *gorm.DB) error {
	return db.AutoMigrate(&model.Cabinet{}, &model.StorageRecord{})
}

func GetLockerByID(db *gorm.DB, id int) (*model.Cabinet, error) {
	var locker model.Cabinet
	result := db.First(&locker, id)
	return &locker, result.Error
}

func UpdateLocker(db *gorm.DB, locker *model.Cabinet) error {
	return db.Save(locker).Error
}

func CreateStorageRecord(db *gorm.DB, record *model.StorageRecord) error {
	return db.Create(record).Error
}

func GetStorageRecordByLockerID(db *gorm.DB, lockerID int) (*model.StorageRecord, error) {
	var record model.StorageRecord
	result := db.First(&record, "locker_id = ?", lockerID)
	return &record, result.Error
}
